/** +----------------------------------------------------------------------
 * | DSSHOP [ 轻量级易扩展低代码开源商城系统 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020~2023 https://www.dswjcms.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed 未经许可不能去掉DSSHOP相关版权
 * +----------------------------------------------------------------------
 * | Author: Purl <383354826@qq.com>
 * +----------------------------------------------------------------------
 */
const filter = {
  '1000': (value) => { // 字符串转货币, 保留二位小数
    if (!value) return 0
    // 获取整数部分
    const intPart = Math.trunc(value)
    // 整数部分处理，增加,
    const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    // 预定义小数部分
    let floatPart = ''
    // 将数值截取为小数部分和整数部分
    const valueArray = value.toString().split('.')
    if (valueArray.length === 2) {
      // 有小数部分
      floatPart = valueArray[1].toString() // 取得小数部分
      return intPartFormat + '.' + floatPart
    }
    return intPartFormat + floatPart
  },
  'smallImage': (img, size) => { // 显示小图片
    const index = img.lastIndexOf('.')
    if (!size) {
      size = 300
    }
    return img.substring(0, index) + '_' + size + '.' + img.substring(index + 1, img.length)
  },
  'numberThousandCarry': (val) => {
    if (parseInt(val) >= 10000) {
      return parseInt(val) / 10000 + 'w'
    } else {
      return parseInt(val)
    }
  }
}

export default filter
